package com.zfl.thread.day04;

import java.util.concurrent.CountDownLatch;

public class ThreadLocalDemo03 {

    public static class House {
        static ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 0);

        volatile int volumn;

        public void add() {
            this.volumn++;
        }

        public void sale() {
            threadLocal.set(threadLocal.get() + 1);
        }

    }


    public static void main(String[] args) throws InterruptedException {
        House house = new House();
        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                int n = (int) (Math.random() * 10);
                for (int j = 0; j < n; j++) {
                    house.add();
                    house.sale();
                }
                System.out.println(Thread.currentThread().getName() + "卖了" + House.threadLocal.get());
                countDownLatch.countDown();
            }, "销售员" + i).start();

        }
        countDownLatch.await();
        System.out.println("总销售额:" + house.volumn);
    }
}
